package ui;

import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.HBox;
import model.LogisticsInfo;
import model.LogisticsHighlight;
import utils.DBUtil;
import utils.PopupUtil;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.control.ScrollPane;
import javafx.stage.Stage;


public class UpdateLogisticsApp extends Application {

    private PopupUtil popupUtil = new PopupUtil();

    // 要更新的客户 ID
    private int customerId;
    
    // 标红信息对象
    private LogisticsHighlight highlight;

    // 提供一个构造函数，传递要更新的客户ID
    public UpdateLogisticsApp(int customerId) {
        this.customerId = customerId;
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("更新客户物流发运需求明细表");

        // 从数据库获取客户信息
        LogisticsInfo logisticsInfo = DBUtil.selectCustomerById(customerId);
        
        // 获取标红信息
        highlight = DBUtil.getLogisticsHighlightByLogisticsId(customerId);
        if (highlight == null) {
            highlight = new LogisticsHighlight(customerId);
        }

        // 创建网格布局并设置样式
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(20, 20, 20, 20));
        grid.setHgap(10);
        grid.setVgap(12);

        // 客户名称
        Label nameLabel = new Label("客户名称:");
        TextField nameField = new TextField(logisticsInfo.getName() != null ? logisticsInfo.getName() : "");
        CheckBox nameHighlight = new CheckBox("标红");
        nameHighlight.setSelected(highlight.getNameHighlight() == 1);
        HBox nameBox = new HBox(10, nameField, nameHighlight);
        nameBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(nameLabel, 0, 0);
        grid.add(nameBox, 1, 0);

        // 仓库地址
        Label addressLabel = new Label("仓库地址:");
        TextField addressField = new TextField(logisticsInfo.getAddress() != null ? logisticsInfo.getAddress() : "");
        CheckBox addressHighlight = new CheckBox("标红");
        addressHighlight.setSelected(highlight.getAddressHighlight() == 1);
        HBox addressBox = new HBox(10, addressField, addressHighlight);
        addressBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(addressLabel, 0, 1);
        grid.add(addressBox, 1, 1);

        // 里程(KM)
        Label kmLabel = new Label("里程 (KM):");
        Spinner<Integer> kmSpinner = new Spinner<>(0, 300, logisticsInfo.getKm());
        kmSpinner.setEditable(true);
        CheckBox kmHighlight = new CheckBox("标红");
        kmHighlight.setSelected(highlight.getKmHighlight() == 1);
        HBox kmBox = new HBox(10, kmSpinner, kmHighlight);
        kmBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(kmLabel, 0, 2);
        grid.add(kmBox, 1, 2);

        //客户联系人
        Label contactsLabel = new Label("客户联系人:");
        TextField contactsField = new TextField(logisticsInfo.getContacts() != null ? logisticsInfo.getContacts() : "");
        CheckBox contactsHighlight = new CheckBox("标红");
        contactsHighlight.setSelected(highlight.getContactsHighlight() == 1);
        HBox contactsBox = new HBox(10, contactsField, contactsHighlight);
        contactsBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(contactsLabel, 0, 3);
        grid.add(contactsBox, 1, 3);

        // 到达要求时段 (HH:mm)
        Label timeLabel = new Label("到达要求时段 (HH:mm):");
        TextField timeField = new TextField(logisticsInfo.getTimeInput() != null ? logisticsInfo.getTimeInput() : "");
        CheckBox timeHighlight = new CheckBox("标红");
        timeHighlight.setSelected(highlight.getTimeInputHighlight() == 1);
        HBox timeBox = new HBox(10, timeField, timeHighlight);
        timeBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(timeLabel, 0, 4);
        grid.add(timeBox, 1, 4);

        // 是否首站
        Label firstStationLabel = new Label("是否需要到达首站:");
        ComboBox<String> firstStationCombo = new ComboBox<>();
        firstStationCombo.getItems().addAll("是", "否");
        // 不在这里设置值，将在Platform.runLater中设置
        firstStationCombo.setEditable(true);
        CheckBox firstStationHighlight = new CheckBox("标红");
        firstStationHighlight.setSelected(highlight.getIsFirstBattleArrivalHighlight() == 1);
        HBox firstStationBox = new HBox(10, firstStationCombo, firstStationHighlight);
        firstStationBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(firstStationLabel, 0, 5);
        grid.add(firstStationBox, 1, 5);

        // 车厢温度要求
        Label truckTempLabel = new Label("车厢温度要求:");
        Spinner<Integer> truckTempMinSpinner = new Spinner<>(-50, 50, logisticsInfo.getTruckTempMin());
        Spinner<Integer> truckTempMaxSpinner = new Spinner<>(-50, 50, logisticsInfo.getTruckTempMax());
        truckTempMinSpinner.setEditable(true);
        truckTempMaxSpinner.setEditable(true);
        CheckBox truckTempMinHighlight = new CheckBox("标红最低");
        truckTempMinHighlight.setSelected(highlight.getTruckTempMinHighlight() == 1);
        CheckBox truckTempMaxHighlight = new CheckBox("标红最高");
        truckTempMaxHighlight.setSelected(highlight.getTruckTempMaxHighlight() == 1);
        
        grid.add(truckTempLabel, 0, 6);
        grid.add(new Label("最低温度:"), 1, 6);
        HBox truckTempMinBox = new HBox(10, truckTempMinSpinner, truckTempMinHighlight);
        truckTempMinBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(truckTempMinBox, 2, 6);
        grid.add(new Label("最高温度:"), 3, 6);
        HBox truckTempMaxBox = new HBox(10, truckTempMaxSpinner, truckTempMaxHighlight);
        truckTempMaxBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(truckTempMaxBox, 4, 6);

        // 产品温度要求
        Label productTempLabel = new Label("产品温度要求:");
        Spinner<Integer> productTempMinSpinner = new Spinner<>(-50, 50, logisticsInfo.getProductTempMin());
        Spinner<Integer> productTempMaxSpinner = new Spinner<>(-50, 50, logisticsInfo.getProductTempMax());
        productTempMinSpinner.setEditable(true);
        productTempMaxSpinner.setEditable(true);
        CheckBox productTempMinHighlight = new CheckBox("标红最低");
        productTempMinHighlight.setSelected(highlight.getProductTempMinHighlight() == 1);
        CheckBox productTempMaxHighlight = new CheckBox("标红最高");
        productTempMaxHighlight.setSelected(highlight.getProductTempMaxHighlight() == 1);
        
        grid.add(productTempLabel, 0, 7);
        grid.add(new Label("最低温度:"), 1, 7);
        HBox productTempMinBox = new HBox(10, productTempMinSpinner, productTempMinHighlight);
        productTempMinBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(productTempMinBox, 2, 7);
        grid.add(new Label("最高温度:"), 3, 7);
        HBox productTempMaxBox = new HBox(10, productTempMaxSpinner, productTempMaxHighlight);
        productTempMaxBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(productTempMaxBox, 4, 7);

        // 测温方式
        Label measureMethodLabel = new Label("测温方式:");
        TextField measureMethodField = new TextField(logisticsInfo.getMeasureMethod() != null ? logisticsInfo.getMeasureMethod() : "");
        CheckBox measureMethodHighlight = new CheckBox("标红");
        measureMethodHighlight.setSelected(highlight.getMeasureMethodHighlight() == 1);
        HBox measureMethodBox = new HBox(10, measureMethodField, measureMethodHighlight);
        measureMethodBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(measureMethodLabel, 0, 8);
        grid.add(measureMethodBox, 1, 8);

        // 温控设备品牌/类型
        Label equipmentLabel = new Label("温控设备品牌/类型:");
        TextField equipmentField = new TextField(logisticsInfo.getEquipmentType() != null ? logisticsInfo.getEquipmentType() : "");
        CheckBox equipmentHighlight = new CheckBox("标红");
        equipmentHighlight.setSelected(highlight.getEquipmentTypeHighlight() == 1);
        HBox equipmentBox = new HBox(10, equipmentField, equipmentHighlight);
        equipmentBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(equipmentLabel, 0, 9);
        grid.add(equipmentBox, 1, 9);

        // 温控设备数量
        Label equipmentNumLabel = new Label("温控设备数量:");
        int equipmentNumValue = 0;
        try {
            if (logisticsInfo.getEquipmentNum() != null) {
                // 尝试解析数字部分，忽略可能的单位（如"台"）
                String numStr = logisticsInfo.getEquipmentNum().replaceAll("[^0-9]", "");
                if (!numStr.isEmpty()) {
                    equipmentNumValue = Integer.parseInt(numStr);
                }
            }
        } catch (NumberFormatException e) {
            // 解析失败时使用默认值0
            equipmentNumValue = 0;
        }
        Spinner<Integer> equipmentNumSpinner = new Spinner<>(0, 100, equipmentNumValue);
        equipmentNumSpinner.setEditable(true);
        CheckBox equipmentNumHighlight = new CheckBox("标红");
        equipmentNumHighlight.setSelected(highlight.getEquipmentNumHighlight() == 1);
        HBox equipmentNumBox = new HBox(10, equipmentNumSpinner, equipmentNumHighlight);
        equipmentNumBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(equipmentNumLabel, 0, 10);
        grid.add(equipmentNumBox, 1, 10);

        // 是否开箱测温
        Label openBoxLabel = new Label("是否开箱测温:");
        ComboBox<String> openBoxCombo = new ComboBox<>();
        openBoxCombo.getItems().addAll("是", "否");
        // 不在这里设置值，将在Platform.runLater中设置
        openBoxCombo.setEditable(true);
        CheckBox openBoxHighlight = new CheckBox("标红");
        openBoxHighlight.setSelected(highlight.getOpenBoxHighlight() == 1);
        HBox openBoxBox = new HBox(10, openBoxCombo, openBoxHighlight);
        openBoxBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(openBoxLabel, 0, 11);
        grid.add(openBoxBox, 1, 11);

        // 是否带托运输
        Label trayTransportLabel = new Label("是否带托运输:");
        ComboBox<String> trayTransportCombo = new ComboBox<>();
        trayTransportCombo.getItems().addAll("是", "否");
        // 不在这里设置值，将在Platform.runLater中设置
        trayTransportCombo.setEditable(true);
        CheckBox trayTransportHighlight = new CheckBox("标红");
        trayTransportHighlight.setSelected(highlight.getTrayTransportHighlight() == 1);
        HBox trayTransportBox = new HBox(10, trayTransportCombo, trayTransportHighlight);
        trayTransportBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(trayTransportLabel, 0, 12);
        grid.add(trayTransportBox, 1, 12);

        // 是否提供卸货服务
        Label unloadingServiceLabel = new Label("是否提供卸货服务:");
        ComboBox<String> unloadingServiceCombo = new ComboBox<>();
        unloadingServiceCombo.getItems().addAll("是", "否");
        // 不在这里设置值，将在Platform.runLater中设置
        unloadingServiceCombo.setEditable(true);
        CheckBox unloadingServiceHighlight = new CheckBox("标红");
        unloadingServiceHighlight.setSelected(highlight.getUnloadingServiceHighlight() == 1);
        HBox unloadingServiceBox = new HBox(10, unloadingServiceCombo, unloadingServiceHighlight);
        unloadingServiceBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(unloadingServiceLabel, 0, 13);
        grid.add(unloadingServiceBox, 1, 13);

        // 卸货服务收费标准(元/吨)
        Label unloadingCostLabel = new Label("卸货服务收费标准(元/吨):");
        TextField unloadingCostField = new TextField(logisticsInfo.getUnloadingCost() != null ? logisticsInfo.getUnloadingCost() : "");
        CheckBox unloadingCostHighlight = new CheckBox("标红");
        unloadingCostHighlight.setSelected(highlight.getUnloadingCostHighlight() == 1);
        HBox unloadingCostBox = new HBox(10, unloadingCostField, unloadingCostHighlight);
        unloadingCostBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(unloadingCostLabel, 0, 14);
        grid.add(unloadingCostBox, 1, 14);

        // 是否需要预约
        Label appointmentLabel = new Label("是否需要预约:");
        ComboBox<String> appointmentCombo = new ComboBox<>();
        appointmentCombo.getItems().addAll("是", "否");
        // 不在这里设置值，将在Platform.runLater中设置
        appointmentCombo.setEditable(true);
        CheckBox appointmentHighlight = new CheckBox("标红");
        appointmentHighlight.setSelected(highlight.getAppointmentHighlight() == 1);
        HBox appointmentBox = new HBox(10, appointmentCombo, appointmentHighlight);
        appointmentBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(appointmentLabel, 0, 15);
        grid.add(appointmentBox, 1, 15);

        // 随车单据何种需求
        Label documentLabel = new Label("随车单据何种需求:");
        TextField documentField = new TextField(logisticsInfo.getDocumentNeed() != null ? logisticsInfo.getDocumentNeed() : "");
        CheckBox documentHighlight = new CheckBox("标红");
        documentHighlight.setSelected(highlight.getDocumentNeedHighlight() == 1);
        HBox documentBox = new HBox(10, documentField, documentHighlight);
        documentBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(documentLabel, 0, 16);
        grid.add(documentBox, 1, 16);

        // 是否盖章签收确认
        Label stampConfirmLabel = new Label("是否盖章签收确认:");
        ComboBox<String> stampConfirmCombo = new ComboBox<>();
        stampConfirmCombo.getItems().addAll("是", "否");
        // 不在这里设置值，将在Platform.runLater中设置
        stampConfirmCombo.setEditable(true);
        CheckBox stampConfirmHighlight = new CheckBox("标红");
        stampConfirmHighlight.setSelected(highlight.getStampConfirmHighlight() == 1);
        HBox stampConfirmBox = new HBox(10, stampConfirmCombo, stampConfirmHighlight);
        stampConfirmBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(stampConfirmLabel, 0, 17);
        grid.add(stampConfirmBox, 1, 17);

        // 湿箱能否更换无批次纸箱
        Label replaceBoxLabel = new Label("湿箱能否更换纸箱:");
        ComboBox<String> replaceBoxCombo = new ComboBox<>();
        replaceBoxCombo.getItems().addAll("是", "否");
        replaceBoxCombo.setEditable(true); // 设置为可编辑
        // 不在这里设置值，将在Platform.runLater中设置
        CheckBox replaceBoxHighlight = new CheckBox("标红");
        replaceBoxHighlight.setSelected(highlight.getReplaceBoxHighlight() == 1);
        HBox replaceBoxBox = new HBox(10, replaceBoxCombo, replaceBoxHighlight);
        replaceBoxBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(replaceBoxLabel, 0, 18);
        grid.add(replaceBoxBox, 1, 18);

        // 异常到达是否可以延迟收货
        Label delayLabel = new Label("异常到达是否可以延迟收货:");
        ComboBox<String> delayCombo = new ComboBox<>();
        delayCombo.getItems().addAll("是", "否");
        delayCombo.setEditable(true); // 设置为可编辑
        // 不在这里设置值，将在Platform.runLater中设置
        CheckBox delayHighlight = new CheckBox("标红");
        delayHighlight.setSelected(highlight.getDelayAcceptHighlight() == 1);
        HBox delayBox = new HBox(10, delayCombo, delayHighlight);
        delayBox.setAlignment(Pos.CENTER_LEFT);
        grid.add(delayLabel, 0, 19);
        grid.add(delayBox, 1, 19);

        // 其他需求
        Label otherLabel = new Label("其他需求:");
        TextArea otherArea = new TextArea(logisticsInfo.getOtherNeeds() != null ? logisticsInfo.getOtherNeeds() : "");
        otherArea.setPrefRowCount(3);
        CheckBox otherHighlight = new CheckBox("标红");
        otherHighlight.setSelected(highlight.getOtherNeedsHighlight() == 1);
        HBox otherBox = new HBox(10, otherArea, otherHighlight);
        otherBox.setAlignment(Pos.TOP_LEFT);
        grid.add(otherLabel, 0, 20);
        grid.add(otherBox, 1, 20, 2, 1);

        // 按钮区域
        HBox buttonBox = new HBox(10);
        buttonBox.setPadding(new Insets(10, 0, 0, 0));

        // 保存按钮
        Button saveButton = new Button("更新");
        saveButton.getStyleClass().add("blue-button");
        saveButton.setOnAction(e -> {
            // 更新数据逻辑
            logisticsInfo.setName(nameField.getText());
            logisticsInfo.setAddress(addressField.getText());
            logisticsInfo.setKm(kmSpinner.getValue());
            logisticsInfo.setContacts(contactsField.getText());
            logisticsInfo.setTimeInput(timeField.getText());
            logisticsInfo.setIsFirstBattleArrival(firstStationCombo.getValue());
            logisticsInfo.setTruckTempMin(truckTempMinSpinner.getValue());
            logisticsInfo.setTruckTempMax(truckTempMaxSpinner.getValue());
            logisticsInfo.setProductTempMin(productTempMinSpinner.getValue());
            logisticsInfo.setProductTempMax(productTempMaxSpinner.getValue());
            logisticsInfo.setMeasureMethod(measureMethodField.getText());
            logisticsInfo.setEquipmentType(equipmentField.getText());
            logisticsInfo.setEquipmentNum(equipmentNumSpinner.getValue().toString());
            logisticsInfo.setOpenBox(openBoxCombo.getValue());
            logisticsInfo.setTrayTransport(trayTransportCombo.getValue());
            logisticsInfo.setUnloadingService(unloadingServiceCombo.getValue());
            logisticsInfo.setUnloadingCost(unloadingCostField.getText());
            logisticsInfo.setAppointment(appointmentCombo.getValue());
            logisticsInfo.setDocumentNeed(documentField.getText());
            logisticsInfo.setStampConfirm(stampConfirmCombo.getValue());
            logisticsInfo.setReplaceBox(replaceBoxCombo.getValue());
            logisticsInfo.setDelayAccept(delayCombo.getValue());
            logisticsInfo.setOtherNeeds(otherArea.getText());
            
            // 更新标红信息
            highlight.setNameHighlight(nameHighlight.isSelected() ? 1 : 0);
            highlight.setAddressHighlight(addressHighlight.isSelected() ? 1 : 0);
            highlight.setKmHighlight(kmHighlight.isSelected() ? 1 : 0);
            highlight.setContactsHighlight(contactsHighlight.isSelected() ? 1 : 0);
            highlight.setTimeInputHighlight(timeHighlight.isSelected() ? 1 : 0);
            highlight.setIsFirstBattleArrivalHighlight(firstStationHighlight.isSelected() ? 1 : 0);
            highlight.setTruckTempMinHighlight(truckTempMinHighlight.isSelected() ? 1 : 0);
            highlight.setTruckTempMaxHighlight(truckTempMaxHighlight.isSelected() ? 1 : 0);
            highlight.setProductTempMinHighlight(productTempMinHighlight.isSelected() ? 1 : 0);
            highlight.setProductTempMaxHighlight(productTempMaxHighlight.isSelected() ? 1 : 0);
            highlight.setMeasureMethodHighlight(measureMethodHighlight.isSelected() ? 1 : 0);
            highlight.setEquipmentTypeHighlight(equipmentHighlight.isSelected() ? 1 : 0);
            highlight.setEquipmentNumHighlight(equipmentNumHighlight.isSelected() ? 1 : 0);
            highlight.setOpenBoxHighlight(openBoxHighlight.isSelected() ? 1 : 0);
            highlight.setTrayTransportHighlight(trayTransportHighlight.isSelected() ? 1 : 0);
            highlight.setUnloadingServiceHighlight(unloadingServiceHighlight.isSelected() ? 1 : 0);
            highlight.setUnloadingCostHighlight(unloadingCostHighlight.isSelected() ? 1 : 0);
            highlight.setAppointmentHighlight(appointmentHighlight.isSelected() ? 1 : 0);
            highlight.setDocumentNeedHighlight(documentHighlight.isSelected() ? 1 : 0);
            highlight.setStampConfirmHighlight(stampConfirmHighlight.isSelected() ? 1 : 0);
            highlight.setReplaceBoxHighlight(replaceBoxHighlight.isSelected() ? 1 : 0);
            highlight.setDelayAcceptHighlight(delayHighlight.isSelected() ? 1 : 0);
            highlight.setOtherNeedsHighlight(otherHighlight.isSelected() ? 1 : 0);
            
            // 验证输入合法性
            if (!validateForm(logisticsInfo)) {
                return;
            }

            // 更新数据库
            DBUtil.updateData(logisticsInfo);
            DBUtil.saveOrUpdateLogisticsHighlight(highlight);
            popupUtil.showAlert(Alert.AlertType.INFORMATION, "更新成功", "客户信息已更新！");
        });

        // 返回按钮
        Button backButton = new Button("返回");
        backButton.getStyleClass().add("blue-button");
        backButton.setOnAction(e -> {
            // 返回主菜单逻辑
            CustomerListPage customerListPage = new CustomerListPage();
            try {
                customerListPage.start(primaryStage);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        buttonBox.getChildren().addAll(saveButton, backButton);
        buttonBox.setAlignment(Pos.BOTTOM_RIGHT);
        grid.add(buttonBox, 1, 21);
        GridPane.setHalignment(buttonBox, HPos.RIGHT);
        GridPane.setValignment(buttonBox, VPos.BOTTOM);
        GridPane.setMargin(buttonBox, new Insets(20, 10, 10, 0));

        // 创建滚动面板
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(grid);
        scrollPane.setFitToWidth(true);
        scrollPane.setFitToHeight(true);
        
        // 设置滚动速度
        scrollPane.addEventFilter(ScrollEvent.ANY, event -> {
            if (event.getDeltaY() != 0) {
                double deltaY = event.getDeltaY() * 3; // 调整滚动速度
                scrollPane.setVvalue(scrollPane.getVvalue() - deltaY / scrollPane.getContent().getBoundsInLocal().getHeight());
                event.consume();
            }
        });

        // 创建场景
        Scene scene = new Scene(scrollPane, 1300, 1000);
        scene.getStylesheets().add(getClass().getResource("/css/form.css").toExternalForm());
        
        primaryStage.setScene(scene);
        primaryStage.show();
        
        // 使用Platform.runLater确保UI组件已完全初始化后再设置值
        Platform.runLater(() -> {
            // 设置所有ComboBox的值
            firstStationCombo.setValue(null);  // 先清空
            firstStationCombo.setValue(logisticsInfo.getIsFirstBattleArrival() != null ? logisticsInfo.getIsFirstBattleArrival() : "否");
            
            openBoxCombo.setValue(null);  // 先清空
            openBoxCombo.setValue(logisticsInfo.getOpenBox() != null ? logisticsInfo.getOpenBox() : "否");
            
            trayTransportCombo.setValue(null);  // 先清空
            trayTransportCombo.setValue(logisticsInfo.getTrayTransport() != null ? logisticsInfo.getTrayTransport() : "否");
            
            unloadingServiceCombo.setValue(null);  // 先清空
            unloadingServiceCombo.setValue(logisticsInfo.getUnloadingService() != null ? logisticsInfo.getUnloadingService() : "否");
            
            appointmentCombo.setValue(null);  // 先清空
            appointmentCombo.setValue(logisticsInfo.getAppointment() != null ? logisticsInfo.getAppointment() : "否");
            
            stampConfirmCombo.setValue(null);  // 先清空
            stampConfirmCombo.setValue(logisticsInfo.getStampConfirm() != null ? logisticsInfo.getStampConfirm() : "否");
            
            replaceBoxCombo.setValue(null);  // 先清空
            replaceBoxCombo.setValue(logisticsInfo.getReplaceBox() != null ? logisticsInfo.getReplaceBox() : "否");
            
            delayCombo.setValue(null);  // 先清空
            delayCombo.setValue(logisticsInfo.getDelayAccept() != null ? logisticsInfo.getDelayAccept() : "否");
        });
    }

    // 验证表单输入的合法性
    private boolean validateForm(LogisticsInfo logisticsInfo) {
        if (logisticsInfo.getName().isEmpty()) {
            popupUtil.showAlert(Alert.AlertType.ERROR, "输入错误", "客户名称不能为空！");
            return false;
        }
        if (logisticsInfo.getAddress().isEmpty()) {
            popupUtil.showAlert(Alert.AlertType.ERROR, "输入错误", "仓库地址不能为空！");
            return false;
        }
        return true;
    }
}
